package me.zhengjie.controller;

import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author krilo
 * @date 2024-09-27 13:22
 */

@RestController
@RequiredArgsConstructor
@Api(tags = "用户信息")
@RequestMapping("/api/ppUser")
public class loginUsrController {

    @GetMapping("/check-login")
    public String checkLoginStatus() {
        // 获取当前的认证对象
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        // 判断是否登录
        if (authentication != null && authentication.isAuthenticated()) {
            // 获取用户信息
            Object principal = authentication.getPrincipal();

            if (principal instanceof UserDetails) {
                UserDetails userDetails = (UserDetails) principal;
                return "当前登录用户: " + userDetails.getUsername();
            } else {
                return "用户未登录";
            }
        } else {
            return "用户未登录";
        }
    }
}
